<template>
  <div class="container flow-column h100">
    <div class="container-main h100">
      <list-table-new v-bind="tableSetting" ref="klTable"></list-table-new>
    </div>
  </div>
</template>
<script>
import { queryPageList } from "@/api/twoTicketManagement/workTicket.js";
export default {
  data() {
    return {
      pageType: "list",
      tableSetting: {
        radioShow: true,
        showFilterBtn: false, // 过滤器
        showCustomBtn: false, // 自定义列
        // 搜索框
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15,
          },
          searchArr: [
            {
              type: "select",
              label: "工单类型",
              model: "orderType",
              option: [
                {
                  label: "缺陷单",
                  value: "defect",
                },
                {
                  label: "正常维护单",
                  value: "maintain",
                },
                {
                  label: "大修",
                  value: "overhaul",
                },
                {
                  label: "试验",
                  value: "experiment",
                },
                {
                  label: "计划预修",
                  value: "repair",
                },
              ],
            },
            {
              type: "organization",
              label: "设备管理单位",
              model: "deptManageName",
              idsStr: "deptManage",
            },
            {
              type: "select",
              label: "专业",
              model: "major",
              placeholder: "请输入",
              option: this.$store.getters["dirtData"]("common_major"),
            },
            {
              type: "select",
              label: "状态",
              model: "statusList",
              multiple: true,
              option: [
                { label: "待执行", value: "WAIT_EXECUTE" },
                { label: "执行中", value: "IN_EXECUTE" },
              ],
            },
            {
              type: "datetimerange",
              label: "计划起止时间",
              model: "estimatedTime",
            },
            {
              type: "input",
              label: "标题",
              model: "title",
              placeholder: "请输入",
            },
            {
              type: "input",
              label: "编号",
              model: "code",
              placeholder: "请输入",
            },
            {
              type: "organization",
              label: "作业单位",
              model: "workUnitName",
              idsStr: "workUnit",
            },
          ],
        },
        // 表头
        column: [
          {
            label: "状态",
            prop: "status",
            formatter: (row) => {
              let types = this.$store.getters["dirtData"](
                "order_defect_status"
              );
              let val = row.status;
              let obj = types.find((i) => i.value == val);
              return obj ? obj.label : val;
            },
          },
          {
            label: "编号",
            prop: "code",
          },
          {
            label: "标题",
            prop: "title",
          },
          {
            label: "设备管理单位",
            prop: "deptManageName",
          },
          {
            label: "作业单位",
            prop: "workUnitName",
          },
          {
            label: "计划开始时间",
            prop: "startTime",
          },
          {
            label: "计划结束时间",
            prop: "endTime",
          },
          {
            label: "工单来源",
            prop: "source",
          },
          {
            label: "创建人",
            prop: "createByName",
          },
          {
            label: "创建时间",
            prop: "createTime",
          },
        ],
        // 表格方法
        tableMethods: {
          //点击行的方法
          rowClick: async (val) => {
            this.$refs.klTable.radioSelect(val);
          },
        },
      },
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let data = {
        page: 1,
        pageSize: 15,
        deptManage: this.$store.state.user.userInfo.orgId,
        deptManageName: this.$store.state.user.userInfo.orgName,
        statusList: ["WAIT_EXECUTE", "IN_EXECUTE"],
      };
      this.tableSetting.searchObj.queryData = data;
      this.tableSetting.searchObj.searchMethod(data);
    },
    // 查询表格数据
    async getTableData(params, sortStr) {
      let queryData = Object.assign({}, params);
      if (queryData.statusList.length == 0) {
        queryData.statusList = ["WAIT_EXECUTE", "IN_EXECUTE"];
      }
      if (queryData.estimatedTime) {
        queryData.startTime = queryData.estimatedTime[0];
        queryData.endTime = queryData.estimatedTime[1];
        delete queryData.estimatedTime;
      }
      let { data } = await queryPageList(queryData);
      data.list.forEach((item) => {
        item.workUnitName = item.deptManageName ? item.deptManageName : "";
        if (item.leaderVo.deptWorkName) {
          item.workUnitName =
            item.workUnitName + "," + item.leaderVo.deptWorkName;
        }
        if (item.orderDispatcherVoList[0].joberJobDeptName) {
          item.workUnitName =
            item.workUnitName +
            "," +
            item.orderDispatcherVoList[0].joberJobDeptName;
        }
        let str = "";
        for (let i = 0; i < item.workUnitName.length; i++) {
          if (str.indexOf(item.workUnitName[i]) == -1) {
            str += item.workUnitName[i];
          }
        }
        let reg = /,$/gi;
        str = str.replace(reg, "");
        item.workUnitName = str;
      });
      this.$set(this.tableSetting, "tableData", data.list || []);
      this.$set(this.tableSetting, "totalNumber", data.total || 0);
    },
    // 返回选中数据
    getSelectData() {
      let selectData = this.$refs.klTable.getRadioData();
      selectData.orderDispatcherVoList.forEach(item => {
        selectData.orderWorkDeptPoList.forEach(item2 => {
          if(item2.workDept === item.workerJobDept) {
            item.type = item2.type
          }
        })
      })
      let val = selectData
        ? {
            director:
              selectData.isOut == 1
                ? selectData.leaderVo.outLeader
                  ? selectData.leaderVo.outLeader
                  : selectData.leaderVo.outLeaderName
                : selectData.leaderVo.leader,
            directorName:
              selectData.isOut == 1
                ? selectData.leaderVo.outLeaderName
                : selectData.leaderVo.leaderName,
            deptId: selectData.isOut == 1 ? "" : selectData.leaderVo.deptWork,
            deptName:
              selectData.isOut == 1 ? "" : selectData.leaderVo.deptWorkName,
            orderLocationList: selectData.orderLocationList,
            orderCode: selectData.code,
            memberNameList: selectData.orderDispatcherVoList,
            orderType: selectData.orderType,
            orderId: selectData.id,
            externalFlag:
              selectData.isOut == 1
                ? selectData.leaderVo.outLeader
                  ? "1"
                  : "-1"
                : "0",
          }
        : null;
      return val;
    },
  },
};
</script>
<style lang="less" scoped>
.flow-column {
  display: flex;
  flex-flow: column nowrap;
}
</style>